package com.zhuss.order.processor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.order.order.SearchOrderBo;
import com.zhuss.bean.bo.order.order.app.DetailTicketBo;
import com.zhuss.bean.unit.order.OrderItemBean;
import com.zhuss.bean.unit.product.TrainBean;
import com.zhuss.bean.vo.order.order.DetailOrderVo;
import com.zhuss.bean.vo.order.order.ListOrderVo;
import com.zhuss.bean.vo.order.order.app.DetailTicketVo;
import com.zhuss.bean.vo.product.train.DetailTrainVo;
import com.zhuss.common.ListVo;
import com.zhuss.common.PageVo;
import com.zhuss.core.db.dao.ext.UserDao;
import com.zhuss.enums.OrderStatus;
import com.zhuss.exception.DzcxException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.order.db.dao.ext.OrderDao;
import com.zhuss.order.db.dao.ext.OrderItemDao;
import com.zhuss.order.db.entity.Order;
import com.zhuss.product.processor.TrainRead;
import com.zhuss.util.ListBean;


/**
 * @description: 订单读
 * @author: zhushanshuo
 * @create: Mon Jan 11 10:24:29 CST 2021
 **/
@Component("orderRead")
public class OrderRead {

	@Autowired
	private TrainRead trainRead;
	
	@Autowired
	private OrderDao orderDao;

	@Autowired
	private OrderItemDao orderItemDao;

	@Autowired
	private UserDao userDao;


	public Map<String, Order> listMap() {
		List<Order> list = orderDao.listAll();
		Map<String, Order> map = new HashMap<>();
		for (Order c : list) {
			map.put(c.getOrderNo(), c);
		}
		return map;
	}

	/**
	 * 订单
	 * @return
	 */
	public Object listOrder() {
		List<Order> orderList = orderDao.listAll();
		List<ListVo> result = new ArrayList<>();
		transferList(orderList, result);
		return result;
	}


	/**
	 * 订单
	 * @return
	 */
	public Object listOrder(List<Long> orderIdList) {
		List<Order> result = orderDao.listById(orderIdList);
		return result;
	}


	/**
	 * 根据名字查询订单
	 * @param
	 * @return
	 */
	public List<Order> list(String name) {
		List<Order> result = orderDao.listByName(name);
		return result;
	}

	public List<Order> listNotId(String name, Long orderId) {
		List<Order> result = new ArrayList<Order>();
		List<Order> list = list(name);
		for (Order c : list) {
			if (!c.getOrderId().equals(orderId)) {
				result.add(c);
				break;
			}
		}
		return result;
	}

	/**
	 * 订单
	 * @return
	 */
	public Object listOrderId() {
		List<Order> orderList = orderDao.listAll();
		List<Long> result = new ArrayList<>();
		transferListId(orderList, result);
		return result;
	}

	private void transferListId(List<Order> orderList, List<Long> result) {
		for (Order item : orderList) {
			result.add(item.getOrderId());
		}
	}

	private void transferList(List<Order> orderList, List<ListVo> result) {
		for (Order item : orderList) {
			ListVo vo = new ListVo();
			vo.setKey(item.getOrderId());
			vo.setLabel(item.getOrderNo());
			result.add(vo);
		}

	}

	/**
	 * 订单列表
	 * @param bo
	 * @return
	 */
	public Object list(SearchOrderBo bo) {
		PageVo<ListOrderVo> result = new PageVo<ListOrderVo>();
        result.setPageNum(bo.getPageNum());
        result.setPageSize(bo.getPageSize());
        result.setItems(transfer(orderDao.list(bo)));
        result.setTotal(orderDao.countList(bo));
		return result;
	}

	private List<ListOrderVo> transfer(List<ListOrderVo> list) {
		for (ListOrderVo item : list) {
			List<OrderItemBean> orderItemList = orderItemDao.listByOrder(item.getOrderId());
			item.setPhone(userDao.selectByPrimaryKey(item.getCreatorId()).getPhone());
			item.setOrderItemList(orderItemList);
		}
		return list;
	}

	/**
	 * 车票
	 * 详情
	 * @param bo
	 * @return
	 * @throws DzcxException
	 */
	public DetailTicketVo detailTicket(DetailTicketBo bo) throws DzcxException {
		Long trainId = bo.getTrainId();
		Long orderId = bo.getOrderId();
		DetailTrainVo train = trainRead.detail(trainId);
		DetailTicketVo result = orderItemDao.getTrain(orderId, trainId, new ListBean<Integer>().build(OrderStatus.支付成功.key()).build(OrderStatus.已确认上车.key()).build(OrderStatus.自动上车.key()).getList());
		if (result == null) {
			throw new DzcxException(ExceptionCode.数据不存在);
		}
		result.setCarId(train.getCarId());
		result.setCarPlateNo(train.getCarPlateNo());
		result.setDriverId(train.getDriverId());
		result.setDriverName(train.getDriverName());
		result.setDriverPhone(train.getDriverPhone());
		return result;
	}
	
	/**
	 * 订单详情
	 * @param orderId
	 * @return
	 * @throws DzcxException
	 */
	public DetailOrderVo detail(Long orderId) throws DzcxException {
		DetailOrderVo result = orderDao.detail(orderId);
		if (result == null) {
			throw new DzcxException(ExceptionCode.数据不存在);
		}
		return result;
	}

	public List<TrainBean> listTrainHasBuy(List<Long> trainIdList, Long userId) {
		List<TrainBean> result = orderItemDao.listTrainHasBuy(trainIdList, userId);
		return result;
	}




}
